﻿package main

import (
    "fmt"
    "strings"
    "bufio"
    "os"
)

func add(n1, n2 string) (sum string) {
    if len(n1) == 0 && len(n2) == 0 {
        sum = "0"
        return
    }
    i1, i2 := len(n1)-1, len(n2)-1
    var left, c1, c2, c, tmp uint8
    for i1 >= 0 && i2 >= 0 {
        c1 = n1[i1] - '0'  // 这样就巧妙的得到了字符对应的数字的int
        c2 = n2[i2] - '0'
        tmp = c1 + c2 + left
        c, left = tmp%10, tmp/10
        //fmt.Println(c, left)
        sum = string(c + '0') + sum
        i1--
        i2--
    }

    c0 := &c1
    i0 := &i1
    n0 := &n1
    if i2 >= 0 {
        c0 = &c2
        i0 = &i2
        n0 = &n2
    }
    for *i0 >= 0 {
        *c0 = (*n0)[*i0] - '0'
        tmp = *c0 + left
        c, left = tmp%10, tmp/10
        sum = string(c + '0') + sum
        *i0--
    }

    if left != 0 {
        tmp = left
        c = tmp%10
        sum = string(c + '0') + sum
    }
    return
}

func main(){
    // 首先是获取终端输入的内容
    fmt.Print("请输入加法算式：")
    reader := bufio.NewReader(os.Stdin)
    result, _, err := reader.ReadLine()
    if err != nil {
        fmt.Println("error:", err)
        return
    }
    
    strSlice := strings.Split(string(result), "+")
    if len(strSlice) != 2 {
        fmt.Println("输入错误")
        return
    }

    n1, n2 := strings.TrimSpace(strSlice[0]), strings.TrimSpace(strSlice[1])
    fmt.Println(add(n1, n2))
}